package com.wechart.service.report.impl;

import com.wechart.dao.domain.Prtryinfo;
import com.wechart.dao.domain.PrtryinfoCriteria;
import com.wechart.dao.domain.Translist;
import com.wechart.dao.domain.TranslistCriteria;
import com.wechart.dao.mapper.PrtryinfoMapper;
import com.wechart.dao.mapper.TranslistMapper;
import com.wechart.dao.mapper.ext.TranslistExtMapper;
import com.wechart.model.ReportModel;
import com.wechart.service.report.TranslistService;
import com.wechart.utils.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by silenceholic on 2017/5/2.
 */
@Service(value = "translistService")
public class TranslistServiceImpl implements TranslistService {

    @Autowired
    private TranslistMapper translistMapper ;

    @Autowired
    private PrtryinfoMapper prtryinfoMapper;

    @Autowired
    private TranslistExtMapper translistExtMapper;

    @Override
    public List<Translist> getAllTranslist(ReportModel reportModel){
        TranslistCriteria translistCriteria = new TranslistCriteria();
        TranslistCriteria.Criteria criteria=translistCriteria.createCriteria();
        if(StringUtils.isNoNullString(reportModel.getBegin())){
            criteria.andWorkdateGreaterThan(StringUtils.cutDateStr(reportModel.getBegin()));
        }
        if(StringUtils.isNoNullString(reportModel.getEnd())){
            criteria.andWorkdateLessThan(StringUtils.cutDateStr(reportModel.getEnd()));
        }
        if(StringUtils.isNoNullString(reportModel.getRefid())){
            criteria.andRefidEqualTo(reportModel.getRefid());
        }
        //业务类型
        if (StringUtils.isNoNullString(reportModel.getPrtry())){
            criteria.andPrtryEqualTo(StringUtils.getIdFromString(reportModel.getPrtry()));
        }
        //发起方发起日期
        if (StringUtils.isNoNullString(reportModel.getSenddate())){
            criteria.andSenddateEqualTo(StringUtils.cutDateStr(reportModel.getSenddate()));
        }
        //协议号
        if (StringUtils.isNoNullString(reportModel.getAgtid())){
            criteria.andAgtidLike(reportModel.getAgtid()+"%");
        }
        //错误码
        if (StringUtils.isNoNullString(reportModel.getErrcode())){
            criteria.andErrcodeLike(reportModel.getErrcode()+"%");
        }
        //发起机构
        if (StringUtils.isNoNullString(StringUtils.getIdFromString(reportModel.getSender()))){
            criteria.andSenderEqualTo(StringUtils.getIdFromString(reportModel.getSender()));
        }
        List<Translist> data = new ArrayList<>();
        RowBounds rowBounds = new RowBounds();
        if (StringUtils.isNoNullString(reportModel.getStart()) && StringUtils.isNoNullString(reportModel.getLength())){
            rowBounds = new RowBounds(Integer.parseInt(reportModel.getStart()),Integer.parseInt(reportModel.getLength()));
        }
        for (Translist translist:translistMapper.selectByExampleWithRowbounds(translistCriteria,rowBounds)){
            String ptrcode = translist.getPrtry();
            PrtryinfoCriteria prtryinfoCriteria = new PrtryinfoCriteria();
            prtryinfoCriteria.createCriteria().andCodeEqualTo(ptrcode);
            translist.setPrtry(prtryinfoMapper.selectByExample(prtryinfoCriteria).get(0).getName());
            data.add(translist);
        }
        return data;
    }
    /**
     * 基本的count
     * @param reportModel
     * @return
     */
    @Override
    public int countTranslist(ReportModel reportModel){
        TranslistCriteria translistCriteria = new TranslistCriteria();
        TranslistCriteria.Criteria criteria=translistCriteria.createCriteria();

        if(StringUtils.isNoNullString(reportModel.getBegin())){
            criteria.andWorkdateGreaterThan(StringUtils.cutDateStr(reportModel.getBegin()));
        }
        if(StringUtils.isNoNullString(reportModel.getEnd())){
            criteria.andWorkdateLessThan(StringUtils.cutDateStr(reportModel.getEnd()));
        }
        if(StringUtils.isNoNullString(reportModel.getRefid())){
            criteria.andRefidEqualTo(reportModel.getRefid());
        }
        //业务类型
        if (StringUtils.isNoNullString(reportModel.getPrtry())){
            criteria.andPrtryEqualTo(StringUtils.getIdFromString(reportModel.getPrtry()));
        }
        //发起方发起日期
        if (StringUtils.isNoNullString(reportModel.getSenddate())){
            criteria.andSenddateEqualTo(StringUtils.cutDateStr(reportModel.getSenddate()));
        }
        //协议号
        if (StringUtils.isNoNullString(reportModel.getAgtid())){
            criteria.andAgtidLike(reportModel.getAgtid()+"%");
        }
        //错误码
        if (StringUtils.isNoNullString(reportModel.getErrcode())){
            criteria.andErrcodeLike(reportModel.getErrcode()+"%");
        }
        //发起机构
        if (StringUtils.isNoNullString(StringUtils.getIdFromString(reportModel.getSender()))){
            criteria.andSenderEqualTo(StringUtils.getIdFromString(reportModel.getSender()));
        }
        return translistMapper.countByExample(translistCriteria);
    }
    /**
     * 流水表物理分页查询
     * @param
     * @return
     */
    @Override
    public List<Translist> selectTranslistByPage(ReportModel reportModel){
        String begin=StringUtils.cutDateStr(reportModel.getBegin());
        String senddate=StringUtils.cutDateStr(reportModel.getSenddate());
        String prtry=StringUtils.getIdFromString(reportModel.getPrtry());
        String sender=StringUtils.getIdFromString(reportModel.getSender());
        List<Translist> data = new ArrayList<>();
        List<Translist> translists = translistExtMapper.selectTranslist(begin,reportModel.getEnd(),reportModel.getRefid(),prtry,
                senddate,reportModel.getAgtid(),reportModel.getErrcode(),sender,reportModel.getStart(),reportModel.getLength());
        for (Translist translist:translists){
            String ptrcode = translist.getPrtry();
            PrtryinfoCriteria prtryinfoCriteria = new PrtryinfoCriteria();
            prtryinfoCriteria.createCriteria().andCodeEqualTo(ptrcode);
            List<Prtryinfo> prtryinfos = prtryinfoMapper.selectByExample(prtryinfoCriteria);
            if (null!=prtryinfos&&prtryinfos.size()>0) {
                translist.setPrtry(prtryinfos.get(0).getName());
            }
            data.add(translist);
        }
        return data;
    }

    public Translist getTranslistByRefid(String refid){
        TranslistCriteria translistCriteria = new TranslistCriteria();
        TranslistCriteria.Criteria criteria=translistCriteria.createCriteria();
        criteria.andRefidEqualTo(refid);
        List<Translist> list=translistMapper.selectByExample(translistCriteria);
        if(list.size()>=1){
            return list.get(0);
        }else {
            return null;
        }
    }

}
